package minatest;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.TransportType;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;

public class MySocketHandler implements IoHandler
{
	public void exceptionCaught(IoSession session,Throwable t)throws Exception
	{
		t.printStackTrace();
		session.close();
	}
	
	@Override
	public void messageReceived(IoSession session,Object msg)throws Exception
	{
		System.out.println("message received");
		String input=msg.toString();
		if(input.equals("quit"))
		{
			session.close();
			return;
		}
		else
		{
			session.write("hello"+input);
			System.out.println("message written..");
		}
	}
	
	public void sessionCreated(IoSession session)throws Exception
	{
		System.out.println("Session created");
		if( session.getTransportType() == TransportType.SOCKET )
			((SocketSessionConfig) session.getConfig() ).setReceiveBufferSize( 2048 );

        session.setIdleTime( IdleStatus.BOTH_IDLE, 10 );

	}

	@Override
	public void messageSent(IoSession arg0, Object arg1) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("message sent...");
	}

	@Override
	public void sessionClosed(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("session closed");
	}

	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("session idle");
	}

	@Override
	public void sessionOpened(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("session open");
	}
}
